//Funcao para remover \n e \r
function removeNL(s){ 
	  return s.replace(/[\n\r\t]/g,""); 
}


jQuery(document).ready(function() {
	$('#datepicker').datepicker();
	jQuery("#results").fadeOut('fast');
	
	jQuery("#search_input").blur(
			function(){
				jQuery("#results").fadeOut('fast');
			}
	);
	
	jQuery("#search_input").focus(
			efetuaBusca
	);
	
	jQuery("#search_input").keyup(
			efetuaBusca
	);
});

function efetuaBusca(){
		//TODO: Validar de acordo com a expressão regular abaixo
		//^[ a-zA-ZçÇáÁéÉíÍóÓúÚâÂêÊîÎôÔûÛãÃẽẼĩĨõÕũŨàÀèÈìÌòÒùÙ]*$
		delay(function(){
			if(jQuery("#search_input").val() == ""){					
				jQuery("#results").fadeOut('fast');								
			}
			
			else{					
				var arrayData = {
					"name": jQuery("#search_input").val(),
					"page": 1,
					"max": 5
				};
				jQuery.ajax( {
					type: 	"POST", 
					url: 	"../findByName.do",
					data:	arrayData,
					success: function(data) {						
						//res = jQuery.parseJSON(data);
						jQuery("#results").fadeIn('fast');
						jQuery("#results").html(data);						
					}
				});
			}								
		}, 300 );
}

function loadContacts(){	
	var arrayData = {
			"id": jQuery("#user_id").val(),
			"max": 10,
			"page": 1,
			"method": "ContactBox"
		};
		jQuery.ajax({
			type: "POST",
			url: 	"../strategy/contact.do",
			data:	arrayData,
			success: function(data) {						
				//res = jQuery.parseJSON(data);			
				jQuery("#common-allies").html(data);
				jQuery("#common-allies").fadeIn('fast');
			}
		});
}

function loadMainStream(page){	
	if(jQuery("#page_stream").val() == "Home")	
		var arrayData = {"method": "home"};
	else
		var arrayData = { "id": jQuery("#user_id").val()};
	
	//Load Main Stream
	jQuery.ajax( {
		type: 	"POST", 
		url: 	"../strategy/action.do",
		data:	arrayData,
		success: function(data) {						
			//res = jQuery.parseJSON(data);			
			jQuery("#main-profile").html(data);
			jQuery("#main-profile").fadeIn('fast');
		}
	});
}

function addContact(id){	
	var arrayData = { "contactId" : id };
	jQuery.ajax({
		type: 	"POST",
		url: 	jQuery("#action").val(),
		data: 	arrayData,
		success: function(data){			
			res = jQuery.parseJSON(data);			
			if(res.success == true){
				loadContacts();
				loadMainStream();
				jQuery("#user-"+id).fadeOut('fast');
				jQuery("#user-"+id).html("OK");
				jQuery("#user-"+id).fadeIn('fast');
			}
		}
	});
}

function saveStatus(event, page){
	var key = event.keyCode;  
    
    if (navigator.appName == 'Microsoft Internet Explorer') {  
        key = event.keyCode;
    } else if (navigator.appName == 'Netscape') {
        key = event.which;
    } 	
	if(key=='13'){
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		if(jQuery("#status").text() != ""){
			var arrayData = {
				"comment": removeNL(jQuery("#status").text()),
				"method": "comment"
			};
			jQuery.ajax( {
				type: 	"POST", 
				url: 	"../strategy/action.do",
				data:	arrayData,
				success: function(data) {
					document.getElementById("status").contentEditable="false";
					loadMainStream(page);
				}
			});
		}
	}     
}

function attacksGetAndLostFocus(){
	jQuery("#attack-comment").focus();
	jQuery("#attack-comment").blur(
			function(){
				jQuery(".attack-comment").remove();
			}		
	);
	
}

function support(id){
	jQuery(".attack-comment").remove();
	jQuery("#allies-entry-"+id).append("<span class='attack-comment'><input id='attack-comment' onKeyDown='javascript:saveSupportDefend(event,"+id+",\"support\");' type='text' /></span>");
	attacksGetAndLostFocus();
}

function defend(id){
	jQuery(".attack-comment").remove();
	jQuery("#allies-entry-"+id).append("<span class='attack-comment'><input id='attack-comment' onKeyDown='javascript:saveSupportDefend(event,"+id+",\"defend\");' type='text' /></span>");
	attacksGetAndLostFocus();
}

function saveSupportDefend(event,id,method){
	var key = event.keyCode;  
    
    if (navigator.appName == 'Microsoft Internet Explorer') {  
        key = event.keyCode;
    } else if (navigator.appName == 'Netscape') {
        key = event.which;
    } 
  
    //alert(method);
    
	if(key=='13'){
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		if(jQuery("#attack-comment").val() != ""){
			//alert(jQuery("#attack-comment").val());
			var arrayData = {
					"comment": jQuery("#attack-comment").val(),
					"method": method,
					"actionIdRel": id
				};
				jQuery.ajax( {
					type: 	"POST", 
					url: 	"../strategy/action.do",
					data:	arrayData,
					success: function(data) {
						jQuery(".attack-comment").remove();
						jQuery("#allies-entry-"+id).append("<span class='attack-comment'>Inserido</span>");
						loadMainStream();
					}
				});
		}
	}    
}

function attack(id, div_id){
	jQuery(".attack-comment").remove();
	jQuery("#"+div_id).append("<span class='attack-comment'><input id='attack-comment' onKeyDown='javascript:saveAttack(event,"+id+");' type='text' /></span>");
	attacksGetAndLostFocus();
}

function saveAttack(event,id){
	var key = event.keyCode;  
    
    if (navigator.appName == 'Microsoft Internet Explorer') {  
        key = event.keyCode;
    } else if (navigator.appName == 'Netscape') {
        key = event.which;
    } 
  
    //alert(key);
    
	if(key=='13'){
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		if(jQuery("#attack-comment").val() != ""){
			var arrayData = {
					"comment": jQuery("#attack-comment").val(),
					"method": "attack",
					"actionIdRel": id
				};
				jQuery.ajax( {
					type: 	"POST", 
					url: 	"../strategy/action.do",
					data:	arrayData,
					success: function(data) {
						jQuery(".attack-comment").remove();
						jQuery("#action-entry-"+id).append("<span class='attack-comment'>Inserido</span>");
						loadMainStream();
					}
				});
		}
	}    
}

function updateImage(){
	jQuery("#image-form").html("");
	jQuery("#image-form").append("<span class='new-image'><input id='new-image' onKeyDown='javascript:saveImage(event);' type='text' /></span>");
}

function saveImage(event){
	var key = event.keyCode;  
    
    if (navigator.appName == 'Microsoft Internet Explorer') {  
        key = event.keyCode;
    } else if (navigator.appName == 'Netscape') {
        key = event.which;
    } 
  
    
	if(key=='13'){
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		if(jQuery("#new-image").val() != ""){
			var arrayData = {
					"profileImg": jQuery("#new-image").val(),
					"method": "changeImage"
				};
			//alert(key);
				jQuery.ajax( {
					type: 	"POST", 
					url: 	"../home/options.do",
					data:	arrayData,
					success: function(data) {
						jQuery("#image-form").html("");
						jQuery("#image-form").append("<span class='attack-comment'>Imagem modificada</span>");
					}
				});
		}
	}    
}

function attackUser(id){
	jQuery(".attack-comment").remove();
	jQuery("#name").append("<span class='attack-comment'><br/><input id='attack-comment' onKeyDown='javascript:saveUserAttack(event,"+id+");' type='text' /></span>");
	attacksGetAndLostFocus();
}

function saveUserAttack(event,id){
	var key = event.keyCode;  
    
    if (navigator.appName == 'Microsoft Internet Explorer') {  
        key = event.keyCode;
    } else if (navigator.appName == 'Netscape') {
        key = event.which;
    } 
  
    //alert(key);
    
	if(key=='13'){
		event.preventDefault ? event.preventDefault() : event.returnValue = false;
		if(jQuery("#attack-comment").val() != ""){
			var arrayData = {
					"comment": jQuery("#attack-comment").val(),
					"method": "attackUser",
					"actionIdRel": id
				};
				jQuery.ajax( {
					type: 	"POST", 
					url: 	"../strategy/action.do",
					data:	arrayData,
					success: function(data) {
						jQuery(".attack-comment").remove();
						jQuery("#name").append("<span class='attack-comment'>Inserido</span>");
						loadMainStream();
					}
				});
		}
	}    
}